<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API Test</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin-bottom: 20px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background-color: #f5f5f5;
            border-radius: 3px;
            white-space: pre-wrap;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        button {
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0069d9;
        }
    </style>
</head>
<body>
    <h1>API 连接测试</h1>
    
    <div class="test-section">
        <h2>健康检查测试</h2>
        <button onclick="testHealthCheck()">测试健康检查</button>
        <div id="health-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h2>模型列表测试</h2>
        <button onclick="testModels()">测试模型列表</button>
        <div id="models-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h2>直接Ollama测试</h2>
        <button onclick="testDirectOllama()">测试直接Ollama连接</button>
        <div id="direct-ollama-result" class="result"></div>
    </div>

    <script>
        async function testHealthCheck() {
            const resultDiv = document.getElementById('health-result');
            resultDiv.textContent = '测试中...';
            
            try {
                console.log('开始健康检查测试');
                const response = await fetch('/v1/llm/ollama/health');
                console.log('健康检查响应:', response);
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const data = await response.json();
                console.log('健康检查数据:', data);
                
                resultDiv.textContent = JSON.stringify(data, null, 2);
                resultDiv.className = 'result success';
            } catch (error) {
                console.error('健康检查错误:', error);
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        async function testModels() {
            const resultDiv = document.getElementById('models-result');
            resultDiv.textContent = '测试中...';
            
            try {
                console.log('开始模型列表测试');
                const response = await fetch('/v1/llm/ollama/models');
                console.log('模型列表响应:', response);
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const data = await response.json();
                console.log('模型列表数据:', data);
                
                resultDiv.textContent = JSON.stringify(data, null, 2);
                resultDiv.className = 'result success';
            } catch (error) {
                console.error('模型列表错误:', error);
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        async function testDirectOllama() {
            const resultDiv = document.getElementById('direct-ollama-result');
            resultDiv.textContent = '测试中...';
            
            try {
                console.log('开始直接Ollama测试');
                const response = await fetch('http://localhost:11434/api/tags');
                console.log('直接Ollama响应:', response);
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const data = await response.json();
                console.log('直接Ollama数据:', data);
                
                resultDiv.textContent = JSON.stringify(data, null, 2);
                resultDiv.className = 'result success';
            } catch (error) {
                console.error('直接Ollama错误:', error);
                resultDiv.textContent = `错误: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
    </script>
</body>
</html>